# coding=utf-8

from django import forms
from django.core.mail import send_mail
from django.http import Http404
from django.http import HttpResponse
from django.shortcuts import render

from easy_blog.models import Blog, Photoprint, Tag, Date_file, Love, Plan


# Create your views here.

class Email(forms.Form):
    email = forms.EmailField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '请输入你的邮箱地址'}))
    name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '请输入你的名字'}))
    textarea = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '请输入你的建议'}))


def email(request):
    if request.method == "POST":
        em = Email(request.POST)
        if em.is_valid():
            email_dir = em.cleaned_data['email']
            email_name = em.cleaned_data['name']
            email_text = em.cleaned_data['textarea']
            send_mail(email_dir, email_text + ' ' + email_name, 'fuyun_hehe@126.com', ['fuyun_hehe@126.com'],
                      fail_silently=False)
            return HttpResponse('ok')
    else:
        em = Email()
    return render(request, "email.html", {'em': em})


def index(request):
    love = Love.objects.all()

    # return render_to_response("index.html", {}, context_instance=RequestContext(request))
    return render(request, 'index.html', {'love': love})


# 解决方案
def plan(request):
    plan = Plan.objects.all()
    return render(request, 'plan.html', {'plan': plan})


def plan_show(request, id=''):
    try:
        plan = Plan.objects.get(id=id)
    except blog.DoesNotExist:
        raise Http404
    return render(request, "plan_show.html", {"blog": plan})


# 全部的博客内容
def blog(request):
    blogs = Blog.objects.all().order_by("-publish_time")
    tags = Tag.objects.all()
    # return render_to_response("my_blog.html", dict(blogs=blogs, tags=tags), context_instance=RequestContext(request))
    return render(request, "my_blog.html", dict(blogs=blogs, tags=tags))


# 博客的标签显示
def blog_tag_show(request, tag):
    blog_tag = Tag.objects.get(tag_name=tag)
    # print tag
    # print blog_tag.id
    blogs = blog_tag.blog_set.all()
    # print blogs
    tags = Tag.objects.all()
    # return render_to_response("my_blog.html", dict(blogs=blogs, tags=tags), context_instance=RequestContext(request))
    return render(request, "my_blog.html", dict(blogs=blogs, tags=tags))


# 博客的详细页面
def blog_show(request, id=''):
    try:
        blog = Blog.objects.get(id=id)
    except blog.DoesNotExist:
        raise Http404
    # return render_to_response("blog_show.html", {"blog": blog}, context_instance=RequestContext(request))
    return render(request, "blog_show.html", {"blog": blog})


def photoprint(request):
    photoprint = Photoprint.objects.all()
    date_file = Date_file.objects.all()
    # return render_to_response("portfolio.html", {"photoprint": photoprint, "date_file": date_file}, context_instance=RequestContext(request))
    return render(request, "portfolio.html", {"photoprint": photoprint, "date_file": date_file})
